package com.weiyuwqj.controller.notice;

import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 统一处理支付的异步回调
 * Created by Administrator on 2017/6/30 0030.
 */
@Controller
public class PaymentController{
    private static Logger log = Logger.getLogger("PaymentController");
    @Autowired
    BusAccountRechargeService accountRechargeService;

    @Autowired
    BusAccountCashService accountCashService;

    @Autowired
    SysContractsService contractsService;
    @Autowired
    BusInvestService investService;
    @Autowired
    BusAccountService busAccountService;
    @Autowired
    BasUserService userService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    BasUserSecurityService userSecurityService;



    /**
     * 网关充值异步通知回调
     * @param
     * @return
     */
    @RequestMapping("/pay/payNotice")
    public void payNotice(HttpServletRequest request, HttpServletResponse response){
       /* log.info("充值回调开始---------------");
        Map<String, String> map = CommonConvert.convertRequestToMap(request);
        String merOrderNo=map.get("merOrderNo");
        log.info("merOrderNo-----------"+merOrderNo);
        BusAccountRecharge busAccountRecharge=accountRechargeService.findBusAccountRechargeByOrderNo(merOrderNo);
        if(busAccountRecharge.getStatus()==2){
            RetData retData=accountRechargeService.platformRechargeSuccess(map);
            log.info("充值retData-----------"+retData);
            //判断通知成功与否
            if(retData.getCode().equals("000000")){
                log.info("处理成功业务-------------------");
                //商户业务
                accountRechargeService.updateBusAccountRechargeStatus(merOrderNo,2,1,"");//充值状态更新
                //商户内部账户扣除费用
                //插入充值费用记录
                BasUser user=userService.selectByKey(busAccountRecharge.getUserId());
                BasUserSecurityLog userSecurityLog=new BasUserSecurityLog();
                userSecurityLog.setAddtime(new Date());
                userSecurityLog.setType(3);
                userSecurityLog.setUserId(busAccountRecharge.getUserId());
                userSecurityLog.setIsdelete(0);
                userSecurityLog.setAmount(BigDecimal.ZERO);
                if(user!=null){
                    //平台用户
                    if(user.getType()==1||user.getType()==2){
                        userSecurityLog.setRemark("个人充值");
                        userSecurityLog.setAmount(busAccountRecharge.getRechargeAmount().multiply(new BigDecimal(0.0015)));
                    }else if(user.getType()==3){
                        userSecurityLog.setRemark("企业充值");
                        userSecurityLog.setAmount(new BigDecimal(10));
                    }
                }else {
                    //sys用户
                    SysUser sysUser=sysUserService.selectByKey(busAccountRecharge.getUserId());
                    if(sysUser!=null&&sysUser.getType()==5){
                        userSecurityLog.setAmount(busAccountRecharge.getRechargeAmount().multiply(new BigDecimal(0.0015)));
                    }else if(sysUser!=null&&user.getType()==2){
                        userSecurityLog.setAmount(new BigDecimal(10));
                    }
                }
                userSecurityService.insertLog(userSecurityLog);
                //通知平台充值成功
                response.setContentType("text/html;charset=UTF-8");
                try {
                    response.getWriter().write(retData.getCode());
                    response.getWriter().flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                log.info("处理失败成功业务-------------------");
                accountRechargeService.updateBusAccountRechargeStatus(merOrderNo,2,0,"");//充值状态更新
            }
        }else {
            //todo 充值状态已经审核
        }
        log.info("充值回调完成-------------------");*/
    }

    /**
     * 委托提现异步通知回调
     */
    @RequestMapping("/pay/cashNotice")
    public void cashNotice(@RequestBody Map<String, String> map,HttpServletResponse response){
       /* log.info("提现回调开始-------------------");
        String merOrderNo=map.get("merOrderNo");
        log.info("提现merOrderNo-------------------"+merOrderNo);
        BusAccountCash busAccountCash=accountCashService.findBusAccountCashByOrderNo(merOrderNo);
        if(busAccountCash.getStatus()==2){
            RetData retData=accountRechargeService.platformRechargeSuccess(map);
            log.info("提现retData-------------------"+retData);
            //判断通知成功与否
            if(retData.getCode().equals("000000")){
                log.info("提现成功业务-------------------");
                //商户业务
                accountCashService.updateBusAccountCashStatus(merOrderNo,2,1,"");//提现状态更新
                //商户内部账户扣除费用
                //插入提现费用记录
                BasUserSecurityLog userSecurityLog=new BasUserSecurityLog();
                userSecurityLog.setAddtime(new Date());
                userSecurityLog.setType(4);
                userSecurityLog.setUserId(busAccountCash.getUserId());
                userSecurityLog.setIsdelete(0);
                userSecurityLog.setRemark("用户提现");
                userSecurityLog.setAmount(BigDecimal.ZERO);
                //T+0
                BigDecimal fee=BigDecimal.ZERO;
                if(busAccountCash.getType()==1){
                    fee=busAccountCash.getCashAmount().multiply(new BigDecimal(0.0005)).add(new BigDecimal(1.50));
                }else if(busAccountCash.getType()==2){
                    fee=new BigDecimal(1.50);
                }
                userSecurityLog.setAmount(fee);
                userSecurityService.insertLog(userSecurityLog);
                //通知平台提现成功
                response.setContentType("text/html;charset=UTF-8");
                try {
                    response.getWriter().write(retData.getCode());
                    response.getWriter().flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                log.info("提现失败业务-------------------");
                accountCashService.updateBusAccountCashStatus(merOrderNo, 2, 0, "");//充值状态更新
            }
        }else {
            //todo 提现状态已经审核
        }
*/
    }

    /**
     * 委托提现异步通知回调
     */
    @RequestMapping("/pay/cashNoticeRepair")
    public void cashNoticeRepair(String merOrderNo,String code,HttpServletResponse response){
      /*  log.info("提现回调开始-------------------");
//        String merOrderNo=map.get("merOrderNo");
        log.info("提现merOrderNo-------------------"+merOrderNo);
        BusAccountCash busAccountCash=accountCashService.findBusAccountCashByOrderNo(merOrderNo);
        if(busAccountCash.getStatus()==2){
//            RetData retData=accountRechargeService.platformRechargeSuccess(map);
//            log.info("提现retData-------------------"+retData);
            //判断通知成功与否
            if(code.equals("000000")){
                log.info("提现成功业务-------------------");
                //商户业务
                accountCashService.updateBusAccountCashStatus(merOrderNo,2,1,"");//提现状态更新
                //商户内部账户扣除费用
                //插入提现费用记录
                BasUserSecurityLog userSecurityLog=new BasUserSecurityLog();
                userSecurityLog.setAddtime(new Date());
                userSecurityLog.setType(4);
                userSecurityLog.setUserId(busAccountCash.getUserId());
                userSecurityLog.setIsdelete(0);
                userSecurityLog.setRemark("用户提现");
                userSecurityLog.setAmount(BigDecimal.ZERO);
                //T+0
                BigDecimal fee=BigDecimal.ZERO;
                if(busAccountCash.getType()==1){
                    fee=busAccountCash.getCashAmount().multiply(new BigDecimal(0.0005)).add(new BigDecimal(1.50));
                }else if(busAccountCash.getType()==2){
                    fee=new BigDecimal(1.50);
                }
                userSecurityLog.setAmount(fee);
                userSecurityService.insertLog(userSecurityLog);
                //通知平台提现成功
                response.setContentType("text/html;charset=UTF-8");
                try {
                    response.getWriter().write(code);
                    response.getWriter().flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                log.info("提现失败业务-------------------");
                accountCashService.updateBusAccountCashStatus(merOrderNo, 2, 0, "");//提现状态更新

            }
        }else {
            //todo 提现状态已经审核
        }*/

    }

    /**
     * 委托充值异步通知回调
     */
    @RequestMapping("/pay/entrustPayNotice")
    public void entrustPayNotice(@RequestBody Map<String, String> map,HttpServletResponse response){
        /*log.info("map------"+JsonUtils.toJson(map));
        String merOrderNo=map.get("merOrderNo");
        BusAccountRecharge busAccountRecharge=accountRechargeService.findBusAccountRechargeByOrderNo(merOrderNo);
        if(busAccountRecharge.getStatus()==2){
            RetData retData=accountRechargeService.platformRechargeSuccess(map);
            //判断通知成功与否
            if(retData.getCode().equals("000000")){
                //商户业务
                accountRechargeService.updateBusAccountRechargeStatus(merOrderNo,2,1,"");//充值状态更新
                //商户内部账户扣除费用
                BasUserSecurityLog userSecurityLog=new BasUserSecurityLog();
                userSecurityLog.setAddtime(new Date());
                userSecurityLog.setType(4);
                userSecurityLog.setUserId(busAccountRecharge.getUserId());
                userSecurityLog.setIsdelete(0);
                userSecurityLog.setRemark("用户充值");
                BigDecimal fee=busAccountRecharge.getRechargeAmount().multiply(new BigDecimal(0.0015));
                if(fee.compareTo(new BigDecimal(2))==-1){
                    fee=new BigDecimal(2);
                }
                userSecurityLog.setAmount(fee);
                //通知平台充值成功
                response.setContentType("text/html;charset=UTF-8");
                try {
                    response.getWriter().write(retData.getCode());
                    response.getWriter().flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                accountRechargeService.updateBusAccountRechargeStatus(merOrderNo,2,0,"");//充值状态更新
            }
        }else {
            //todo 充值状态已经审核
        }*/
    }

    //测试
    //todo delete
    @ResponseBody
    @RequestMapping(value = "/testContract")
    public Object testContract(MultipartFile file,Integer type){
        File f = null;
        try {
            f=File.createTempFile("tmp", null);
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
         return null ;//contractsService.GenerateContractTemplates(f,type);
        //contractsService.addPersonSealResult("孙剑","33012219910928001X","18815292330",63);
        //contractsService.generateContracts(4002);
        //contractsService.addPersonSealResult("林安楷","330329199508094631","15168300521",45);
//        List<BusItemInvest> investList =investService.listItemInvestNoContracts();
//        for (BusItemInvest invest: investList){
//            try {
//                contractsService.generateContracts(invest);
//            }catch (Exception e){
//                System.out.println("investId"+invest.getId()+e);
//                continue;
//            }
//        }
        //return null;
    }

    /**
     * 法大大单独开户接口
     * @param realname
     * @param idCard
     * @param mobile
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/fadadaRalname")
    public Object fadadaRalname(String realname,String idCard,String mobile,Integer userId){
       // Map map=contractsService.addPersonSealResult(realname,idCard,mobile,userId);
        return null;
    }

/*    @ResponseBody
    @RequestMapping(value = "/fadada/notify")
    public Object fadadaNotify(NotifyParam notifyParam){
        if(notifyParam.getResult_code().equals("3000")){
            contractsService.contractFiling(JsonUtils.json2Map(JsonUtils.toJson(notifyParam)));
        }
        return null;
    }*/








}
